package org.link.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.link.bean.User;
import org.link.service.ArticleService;
import org.link.service.BoardService;
import org.link.service.UserService;
import org.link.util.MDUtil;
import org.link.util.SendEmail;

/**
 * 
 * project_name:forumweb
 * package_name:org.link.web
 * file_name:UserServlet.java
 * @author Link
 * create_date:2015年10月22日 下午2:02:49
 * modified_date:
 * description: 用户信息处理selvlet类
 *
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserService();
	private BoardService boardService = new BoardService();
	private ArticleService articleService = new ArticleService();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String option = request.getParameter("op");
		HttpSession session = request.getSession();
		if(option.equals("managerLogin")){
			managerLoginServlet(request, response,session);
		}else if(option.equals("checkUserName")){
			checkUserNameServlet(request, response,session);
		}else if(option.equals("registerUser")){
			addUserServlet(request, response,session);
		}else if(option.equals("activeUser")){
			activeUserServlet(request, response,session);
		}else if(option.equals("userLogin")){
			userLoginServlet(request, response,session);
		}else if(option.equals("checkUserNameAndPass")){
			userLoginCheckServlet(request, response,session);
		}else if(option.equals("userLoginOut")){
			userLoginOutServlet(request, response,session);
		}
		
	}
	
	/**
	 * 
	 * description: 用户登出信息修改
	 * 2015年10月26日 下午4:42:03
	 * @author Link
	 * @param request
	 * @param response
	 * @param session
	 * @throws IOException 
	 *
	 */
	private void userLoginOutServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		String email = request.getParameter("email");
		int rows = 4;		//显示行数
		int beginIndex = 0;//起始显示索引位置
		int totalPage = 0;//总页数
		int allMainArticCount = articleService.articleCount(0);//主贴子总数
		if(allMainArticCount%rows==0){
			totalPage = allMainArticCount/rows;
		}else{
			totalPage = allMainArticCount/rows+1;
		}
		if(userService.userLoginOut(email)==1){
			session.setAttribute("userInfo", null);
			session.setAttribute("curPage", 1);//当前页数
			session.setAttribute("totalPage", totalPage);//总共页数
			session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
			session.setAttribute("boardInfo", boardService.queryAllChild());//查询出所有父板块对应的所以子版块的信息并保存到session中
			response.sendRedirect("user-index.jsp");
		}
	}

	/**
	 * 
	 * description: 输入的用户名和密码判断
	 * creatTime:2015年10月25日上午10:33:54
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void userLoginCheckServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		PrintWriter out = response.getWriter();
		if(userService.checkUserEmailAndPass(email, MDUtil.encodePass(pass))==1){
			out.print("emailError");
		}else if(userService.checkUserEmailAndPass(email, MDUtil.encodePass(pass))==2){
			out.print("passError");
		}else{
			out.print("ok");
		}
		
	}

	/**
	 * 
	 * description: 用户登录!
	 * creatTime:2015年10月25日上午10:23:34
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void userLoginServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		session.setAttribute("activeTips", null); //	将邮箱激活提示设置为空
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		int rows = 4;		//显示行数
		int beginIndex = 0;//起始显示索引位置
		int totalPage = 0;//总页数
		int allMainArticCount = articleService.articleCount(0);//主贴子总数
		if(allMainArticCount%rows==0){
			totalPage = allMainArticCount/rows;
		}else{
			totalPage = allMainArticCount/rows+1;
		}
		String ip = request.getRemoteAddr();
		//User user = userService.userLoginInfo(email, MDUtil.encodePass(pass), ip);//获取用户信息并update ip字段
		if(request.getParameter("rememberUser")!=null){		//设置记住密码选项框cookie事件
			Cookie emailCookie = new Cookie("user1", email);
			emailCookie.setMaxAge(3600*7*24);
			response.addCookie(emailCookie);
			Cookie passCookie = new Cookie("user2", MDUtil.encodePass(pass));
			passCookie.setMaxAge(3600*24*7);
			response.addCookie(passCookie);
		}
		session.setAttribute("userInfo", userService.userLoginInfo(email, MDUtil.encodePass(pass), ip));//设置user信息的session属性以便主界面提取信息显示
		session.setAttribute("curPage", 1);//当前页数
		session.setAttribute("totalPage", totalPage);//总共页数
		session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
		session.setAttribute("boardInfo", boardService.queryAllChild());//查询出所有父板块对应的所以子版块的信息并保存到session中
		response.sendRedirect("user-index.jsp");//跳转到主界面
	}

	/**
	 * 
	 * description: 激活注册用户
	 * creatTime:2015年10月24日下午10:15:17
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void activeUserServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		String email = request.getParameter("email");
		if(userService.activeUser(email)==1){	//激活成功
			session.setAttribute("tips", null);//将验证码提示消息置空
			session.setAttribute("activeTips", "邮箱激活成功,请登录!");
			response.sendRedirect("pages/user-login.jsp");
		}
	}

	/**
	 * 
	 * description: 添加注册用户
	 * creatTime:2015年10月24日下午4:02:09
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 * @throws ServletException 
	 *
	 */
	private void addUserServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		String code = request.getParameter("registerCode");
		if(!code.equals((String)session.getAttribute("imageCode"))){
			session.setAttribute("tips", "验证码错误!");
			response.sendRedirect("pages/user-regist.jsp");
		}else{
			String email = request.getParameter("registerEmail");
			String pass = request.getParameter("registerPass");
			if(userService.addUser(email, MDUtil.encodePass(pass))==1){
				session.setAttribute("tips", email+"邮箱注册成功,请速去邮箱激活!");
				new SendEmail(email, "http://192.168.20.184:8080/forumweb/UserServlet?op=activeUser&email="+email).start();
				request.getRequestDispatcher("pages/user-active-tip.jsp").forward(request, response);
			}
		}
	}

	/**
	 * 
	 * description: 判断用户名是否已注册过
	 * creatTime:2015年10月24日下午2:27:49
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void checkUserNameServlet(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("registerEmail");
		if(userService.checkUserNameExist(name)){
			out.print("exist");
		}else{
			out.print("inexistence");
		}
		
	}

	/**
	 * 
	 * description: 管理员登录处理方法体
	 * 2015年10月22日 下午2:54:31
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 *
	 */
	protected void managerLoginServlet(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws ServletException, IOException{
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		String ip = request.getRemoteAddr();//获取客户端的ip地址
		User user = new User();
		user.setEmail(email);
		user.setPass(MDUtil.encodePass(pass));
		user.setIp(ip);
		int result = userService.managerLogin(user);
		if(result==1){
			session.setAttribute("manaLoginMessage", "用户名输入不正确!");
			request.getRequestDispatcher("manager-index.jsp").forward(request, response);
		}else if(result==2){
			session.setAttribute("manaLoginMessage", "密码错误!");
			request.getRequestDispatcher("manager-index.jsp").forward(request, response);
		}else{
			if(request.getParameter("rememberManager")!=null){
				Cookie emailCookie = new Cookie("hello1", email);
				emailCookie.setMaxAge(3600*30*24);
				response.addCookie(emailCookie);
				Cookie passCookie = new Cookie("hello2", MDUtil.encodePass(pass));
				passCookie.setMaxAge(3600*24*30);
				response.addCookie(passCookie);
			}
			session.setAttribute("allBoard", boardService.QueryAllBoard());
			session.setAttribute("allFatherBoard", boardService.queryAllFatherBoard());
			response.sendRedirect("pages/home.jsp");	
		}
	}

}










